package com.example.yiwei.crm.view.Class.listviewClass;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.entity.ExchangeEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by DHN on 2017/6/12.
 */

public class ExchangeAdapter extends BaseAdapter {
    private Context context;
    public static List<ExchangeEntity> beans;
    // 用来控制CheckBox的选中状况
    private static HashMap<Integer, Boolean> isSelected;

    class ViewHolder {
        TextView sq_time;
        TextView bf_time;
        TextView tvName;
        TextView bf_name;
        CheckBox cb;
    }

    public ExchangeAdapter(Context context, List<ExchangeEntity> beans) {
        // TODO Auto-generated constructor stub
        this.beans = beans;
        this.context = context;
        isSelected = new HashMap<Integer, Boolean>();
        // 初始化数据
        initDate();
    }


    // 初始化isSelected的数据
    public static void initDate() {
        for (int i = 0; i < beans.size(); i++) {
            getIsSelected().put(i, false);
        }
    }
    public int getCount() {
        // TODO Auto-generated method stub
        return beans.size();
    }
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return beans.get(position);
    }
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        // 页面
        ViewHolder holder;
       ExchangeEntity bean = beans.get(position);
        LayoutInflater inflater = LayoutInflater.from(context);
        if (convertView == null) {
            convertView = inflater.inflate(
                    R.layout.layout_item_jl, null);
            holder = new ViewHolder();
            holder.cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
            holder.tvName = (TextView) convertView
                    .findViewById(R.id.tv_jl_people);
            holder.sq_time = (TextView) convertView.findViewById(R.id.tv_sq_time);
            holder.bf_time = (TextView) convertView.findViewById(R.id.tv_bf_time);
            holder.bf_name= (TextView) convertView.findViewById(R.id.tv_bf_name);
            convertView.setTag(holder);
        } else {
            // 取出holder
            holder = (ViewHolder) convertView.getTag();
        }
            // 监听checkBox并根据原来的状态来设置新的状态
        holder.cb.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

                    if (isSelected.get(position)) {
                        isSelected.put(position, false);
                        setIsSelected(isSelected);
                    } else {
                        isSelected.put(position, true);
                        setIsSelected(isSelected);
                    }

                }
            });

        holder.tvName.setText( bean.getManger_name());
        holder.sq_time.setText(bean.getSq_time());
        holder.bf_time.setText(bean.getBf_time());
        holder.bf_name.setText(bean.getFs_name());

        if (getIsSelected().get(position)!=null){

        }else {
            getIsSelected().put(position, false);
        }
        // 根据isSelected来设置checkbox的选中状况
        holder.cb.setChecked(getIsSelected().get(position));
        return convertView;
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        isSelected = isSelected;
    }
    public static List<Integer> getIDs(){
        List<Integer> list  = new ArrayList<>();
        for (int i = 0;i<beans.size();i++){
            if (getIsSelected().get(i)!=null){

            }else {
                getIsSelected().put(i, false);
            }
            if(isSelected.get(i)){
                list.add(beans.get(i).getId());
            }
        }
        return list;
    }
    public static void setState(){
        for (int i = 0;i<beans.size();i++){
            if(isSelected.get(i)){
                isSelected.put(i, false);
            }
        }
    }
}
